library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(haven)
library(ggplot2)

# Figure 8.7
df.anes <- read_sav("C:/Users/Robert Allred/Documents/Duke/Civ_Mil_Appendix/Data/anes_timeseries_cdf.sav")

df.anes$DEM <- 0
df.anes$DEM[df.anes$VCF0303 == 1] <- 1

df.anes$REP <- 0
df.anes$REP[df.anes$VCF0303 == 3] <- 1

df.anes$IND <- 0
df.anes$IND[df.anes$VCF0303 == 2] <- 1

df.anes$post911 <- 0
df.anes$post911[df.anes$VCF0004 > 2000] <- 1

df.anes.dem <- df.anes[df.anes$VCF0843 < 8 & df.anes$DEM == 1 & !is.na(df.anes$VCF0843),]
df.anes.rep <- df.anes[df.anes$VCF0843 < 8 & df.anes$REP == 1 & !is.na(df.anes$VCF0843),]

# Line plot without points
ggdem <- ggplot(data=df.anes.dem, aes(y=VCF0843,x=VCF0213))+
  geom_smooth(method='lm',mapping = aes(group=post911,weight = VCF0009z))+
  scale_y_continuous(limits=c(0,7))+
  scale_x_continuous(limits=c(0,100))+
  xlab("\nThermometer - Military")+
  ylab("Support for Defense Spending\n")+
  ggtitle("Democrats")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=16), legend.position = "bottom", 
        legend.title = element_blank())


ggrep <- ggplot(data=df.anes.rep, aes(y=VCF0843,x=VCF0213))+
  geom_smooth(method='lm',mapping = aes(group=post911,weight = VCF0009z))+
  scale_y_continuous(limits=c(0,7))+
  scale_x_continuous(limits=c(0,100))+
  xlab("\nThermometer - Military")+
  ylab("")+
  ggtitle("Republicans")+
  theme(panel.grid = element_blank(), panel.background = element_blank(),
        panel.border = element_rect(fill=NA),
        text = element_text(size=16), legend.position = "bottom", 
        legend.title = element_blank())

grid.arrange(ggdem,ggrep,ncol=2)
